<?php

namespace App\Wxapp\Controllers;

use App\Store\Models\Member\Member;
use App\Store\Models\Sysset\Config;
use App\Store\Models\Wxapp\WxappAddress;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 个人中心相关
 */
class AddressController extends BaseController
{
    public function getlist(Request $request){
        $list=WxappAddress::getListByMem($this->cp_id,$this->meminfo['id']);
        return response()->json(['code' => '000', 'msg' => '获取成功','data'=>$list]);
    }


     /**
     * 删除
     */
    public function getinfo(Request $request)
    {
        $id=intval($request->id);
        $info=WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id'],'id'=>$id,'is_del'=>1])->first();
        if(!$info){
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功','data'=>['info'=>$info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request){
        $req_data=$request->only(['real_name','mobile','address','is_default']);
        if(!isset($req_data['real_name']) || $req_data['real_name']==''){

            return response()->json(['code' => '001', 'msg' => '姓名不能为空']);
        }

        if(!isset($req_data['mobile']) || $req_data['mobile']==''){

            return response()->json(['code' => '001', 'msg' => '手机号不能为空']);
        }

        if(!isset($req_data['address']) || $req_data['address']==''){

            return response()->json(['code' => '001', 'msg' => '详细地址不能为空']);
        }

        $data=$req_data;
        
        $data['cp_id']=$this->cp_id;
        $data['mem_id']=$this->meminfo['id'];
        $data['is_default']=isset($req_data['is_default'])?($req_data['is_default']==1?1:2):1;
        $data['create_at']=time();
        $data['update_at']=time();
        $ins_id=WxappAddress::create($data)->id;
        if($data['is_default']==2){
            WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id']])->where('id','<>',$ins_id)->update(['is_default'=>1]);
        }
        return response()->json(['code' => '000', 'msg' => '添加成功']);
    }
    /**
     * 编辑
     */
    public function dopostedit(Request $request){
        $req_data=$request->only(['real_name','mobile','address','is_default']);
        $id=intval($request->id);
        $info=WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id'],'id'=>$id])->first();
        if(!$info){
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        if(!isset($req_data['real_name']) || $req_data['real_name']==''){

            return response()->json(['code' => '001', 'msg' => '姓名不能为空']);
        }

        if(!isset($req_data['mobile']) || $req_data['mobile']==''){

            return response()->json(['code' => '001', 'msg' => '手机号不能为空']);
        }

        if(!isset($req_data['address']) || $req_data['address']==''){

            return response()->json(['code' => '001', 'msg' => '详细地址不能为空']);
        }

        $data=$req_data;
        $data['is_default']=isset($req_data['is_default'])?($req_data['is_default']==1?1:2):1;
        $data['update_at']=time();
        WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id'],'id'=>$id])->update($data);
        if($data['is_default']==2){
            WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id']])->where('id','<>',$id)->update(['is_default'=>1]);
        }
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $id=intval($request->id);
        $info=WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id'],'id'=>$id,'is_del'=>1])->first();
        if(!$info){
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        
        WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id'],'id'=>$id])->update(['is_del'=>2,'del_at'=>time()]);
        return response()->json(['code' => '000', 'msg' => '删除成功']);
    }

    /**
     * 设为默认
     */
    public function dosetdefault(Request $request)
    {
        $id=intval($request->id);
        $info=WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id'],'id'=>$id])->first();
        if(!$info){
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        
        WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id'],'id'=>$id])->update(['is_default'=>2,'update_at'=>time()]);
        WxappAddress::where(['cp_id'=>$this->cp_id,'mem_id'=>$this->meminfo['id']])->where('id','<>',$id)->update(['is_default'=>1]);
        return response()->json(['code' => '000', 'msg' => '设置成功']);
    }
}